//
//  SearchResultViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SearchResultViewController.h"

@interface SearchResultViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.strQ;
    self.page = 1;
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self getData];
    [self creatTableView];
    self.navigationItem.rightBarButtonItem = nil;
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
}


#pragma mark - 建立tableView
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[LeftPandectCustomTableViewCell class] forCellReuseIdentifier:@"LeftPandectCustomTableViewCellIdentifier"];
    
    //上拉加载
    self.tableView.mj_footer = [MJChiBaoZiFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        
    [self.view addSubview: self.tableView];
}

#pragma mark - tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LeftPandectCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftPandectCustomTableViewCellIdentifier"];
    if (self.dataSource.count != 0) {
        CookDetaileModel *detaileModel = self.dataSource[indexPath.row];
        cell.detaileModel = detaileModel;
    }
    
    //取消选中
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

//动画 类似果冻弹动
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    cell.alpha = 0.5;
    
    CGAffineTransform transformScale = CGAffineTransformMakeScale(0.8, 0.8);
    CGAffineTransform transformTranslate = CGAffineTransformMakeTranslation(0.8, 0.8);
    cell.transform = CGAffineTransformConcat(transformScale, transformTranslate);
    
    [tableView bringSubviewToFront:cell];
    [UIView animateWithDuration:.4f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        cell.alpha = 1;
        cell.transform = CGAffineTransformIdentity;
    } completion:nil];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //跳转详情页面
    CookDetaileModel *detailModel = self.dataSource[indexPath.row];
    CookDetailsViewController *detaileVC = [[CookDetailsViewController alloc] init];
    detaileVC.strCode = detailModel.code;
    
    [self.navigationController pushViewController:detaileVC animated:YES];
}

#pragma mark - 获取数据
- (void)getData {
    
    NSString *strUrl = [NSString stringWithFormat:@"http://food.boohee.com/fb/v1/foods/search?q=caomei&page=%ld", self.page];
    NSDictionary *dic = @{@"q" : self.strQ};
    [FSDNetWorkTool getWithURL:strUrl withParameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *arr = result[@"foods"];
        //返回值不为空
        if (arr.count) {
            for (NSDictionary *dic in arr) {
                CookDetaileModel *detaileModel = [[CookDetaileModel alloc] initWithDictionary:dic];
                [self.dataSource addObject:detaileModel];
            }
            
            [self.tableView reloadData];
            self.HUD.hidden = YES;
            self.page++;
        } else {
           
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有搜索到内容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alert show];
            
            self.HUD.hidden = YES;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        
    } fail:^(NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有搜索到内容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        
        self.HUD.hidden = YES;
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

        [self.navigationController popToRootViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
